<?php 
include('header.php');
// Query the database for enabled and disabled servers
$servers_en = mysql_query('SELECT * FROM server WHERE disabled=0 ORDER BY hostname ASC');
$servers_ne = mysql_query('SELECT * FROM server WHERE disabled=1 ORDER BY hostname ASC');
?>
<h2>Overview</h2>
<hr />
<h3>Enabled</h3>
<span class="red_bg width250">Out of date. Updates not scheduled.</span>
<span class="yellow_bg width250">Out of date. Updates scheduled.</span>
<span class="green_bg width250">Up to date. Updates not needed.</span>
<br /><br />
<table class="bigtable">
	<tbody>
		<tr class="row1 bold">
			<td>Hostname</td>
			<td># Updates Available</td>
			<td>Update @ Next Runtime</td>
			<td>Last Check</td>
			<td>Last Update</td>
			<td>Last Exit Status</td>
		</tr>
		<?php $n = 0; // Set n for alternating row colors in tables ?>
		<?php while($serversrow_en = mysql_fetch_array($servers_en)) { // Loop through query results?>
		<?php // Set variables from row array ?>
		<?php $id = $serversrow_en['id']; ?>
		<?php $hostname = $serversrow_en['hostname']; ?>
		<?php $packages = $serversrow_en['packages']; ?>
		<?php $last_check = $serversrow_en['last_check']; ?>
		<?php $run_update = $serversrow_en['run_update']; ?>
		<?php $exit_stat = $serversrow_en['exit_stat']; ?>
		<?php $last_update = $serversrow_en['last_update']; ?>
		<?php $reboot = $serversrow_en['reboot']; ?>
	        <?php
		// Set the tr class for rotating colors in tables
        	if($n % 2 == 1) {
                        $row_class = 'row1';
	        } else {
        	        $row_class = 'row2';
                }
        	?>
		<tr class="<?php echo $row_class; ?>">
			<td <?php 
				// echo the hostname in color depending on the update status
				if (($packages) && ($run_update == 0)) { 
					echo 'class="red_bg"';
				} elseif (($packages) && ($run_update == 1)) {
					echo 'class="yellow_bg"';
				} else {
					echo 'class="green_bg"';
				} ?>><?php echo '<a href="srvinfo.php?id=' . $id . '">' . $hostname . '</a>'; ?></td>
			<td><?php if ($packages) { // echo number of available updates
					echo count(explode(' ', $packages)); 
				} else {
					echo "0";
				} ?></td>
			<td><?php if ($run_update == 1) { if ($reboot == 1) { echo 'Yes (reboot)'; } else { echo 'Yes'; } } else { echo 'No'; } // echo if updates are scheduled ?></td>
			<td><?php if ($last_check) { echo $last_check; } else { echo 'Never'; } // Echo the last check date ?></td>
			<td><?php if ($last_update) { echo $last_update; } else { echo 'Never'; } // echo the last update date ?></td>
			<td><?php if ($exit_stat == 0) { echo 'Sucessful'; } else { echo 'Failed'; } // echo the last exit status ?></td>
		</tr>
		<?php $n++; ?>
		<?php } ?>
	</tbody>
</table>

<hr />
<h3>Disabled</h3>
<span class="red_bg width250">Out of date. Updates not scheduled.</span>
<span class="yellow_bg width250">Out of date. Updates scheduled.</span>
<span class="green_bg width250">Up to date. Updates not needed.</span>
<br /><br />
<table class="bigtable">
        <tbody>
                <tr class="row1 bold">
                        <td>Hostname</td>
                        <td># Updates Available</td>
                        <td>Update @ Next Runtime</td>
                        <td>Last Check</td>
			<td>Last Update</td>
                        <td>Last Exit Status</td>
                </tr>
                <?php $n = 0; ?>
                <?php while($serversrow_ne = mysql_fetch_array($servers_ne)){ ?>
                <?php $id = $serversrow_ne['id']; ?>
                <?php $hostname = $serversrow_ne['hostname']; ?>
                <?php $packages = $serversrow_ne['packages']; ?>
                <?php $last_check = $serversrow_ne['last_check']; ?>
                <?php $run_update = $serversrow_ne['run_update']; ?>
                <?php $exit_stat = $serversrow_ne['exit_stat']; ?>
		<?php $last_update = $serversrow_ne['last_update']; ?>
		<?php $reboot = $serversrow_ne['reboot']; ?>
                <?php
                if($n % 2 == 1) {
                        $row_class = 'row1';
                } else {
                        $row_class = 'row2';
                }
                ?>
                <tr class="<?php echo $row_class; ?>">
                        <td <?php
                                if (($packages) && ($run_update == 0)) {
                                        echo 'class="red_bg"';
                                } elseif (($packages) && ($run_update == 1)) {
                                        echo 'class="yellow_bg"';
                                } else {
                                        echo 'class="green_bg"';
                                } ?>><?php echo '<a href="srvinfo.php?id=' . $id . '">' . $hostname . '</a>'; ?></td>
                        <td><?php if ($packages) {
                                        echo count(explode(' ', $packages));
                                } else {
                                        echo "0";
                                } ?></td>
                        <td><?php if ($run_update == 1) { if ($reboot == 1) { echo 'Yes*'; } else { echo 'Yes'; } } else { echo 'No'; } ?></td>
                        <td><?php if ($last_check) { echo $last_check; } else { echo 'Never'; } ?></td>
			<td><?php if ($last_update) { echo $last_update; } else { echo 'Never'; } ?></td>
                        <td><?php if ($exit_stat == 0) { echo 'Sucessful'; } else { echo 'Failed'; } ?></td>
                </tr>
                <?php $n++; ?>
                <?php } ?>
        </tbody>
</table>

<hr />

<?php include('footer.php'); ?>
